<template>
  <div>
    <div style="width: 400px; height: 300px; background-color: aqua;margin: 150px auto;border-radius: 10px">
      <div style="text-align: center; color: green; height: 50px; line-height: 70px">欢迎注册</div>
      <div style="text-align: center; color: blue;margin-top: 50px">
        <el-form :model="admin" label-width="80px">
          <el-form-item label="账号" style="margin-left: 10px">
            <el-input v-model="admin.name" prefix-icon="el-icon-user" placeholder="请输入用户名" style="width: 80%;"></el-input>
          </el-form-item>
          <el-form-item label="密码" style="margin-left: 10px">
            <el-input type="password" v-model="admin.password" prefix-icon="el-icon-lock" placeholder="请输入密码" style="width: 80%;"></el-input>
          </el-form-item>
          <el-form-item label="角色" style="margin-left: 10px">
            <el-select v-model="admin.role" placeholder="请选择" style="width: 80%;">
              <el-option label="学生" value="ROLE_STUDENT"></el-option>
              <el-option label="教师" value="ROLE_TEACHER"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="register()">注册</el-button>
          </el-form-item>
        </el-form>
        已有账号？<a href="javascript:void(0);" style="text-decoration: none" @click="navLogin">点击登录</a>
      </div>
    </div>
  </div>
</template>


<script>

import request from "@/utils/request";

export default {
  name: 'Register',
  data() {
    return {
      admin:{},
    }
  },
  // 页面加载时
  created() {

  },
  // 页面按钮触发事件时使用的方法
  methods: {
    //1. 注册
    register(){
      // this.admin.role = 'ROLE_STUDENT';
      request.post('/admin/register',this.admin).then(res => {
            if (res.code === '0'){
              this.$message({
                message:'注册成功',
                type: 'success'
              })
              // 登录成功，跳转登录页
              this.$router.push('/login');
            }else {
              this.$message({
                message: res.msg,
                type: 'error'
              })
            }
          }
      )
    },

    //2.去登录
    navLogin(){
      this.$router.push('/login');
    }
  }
}
</script>
